package com.chixing.service.impl;

import com.chixing.entity.*;
import com.chixing.mapper.FlowMapper;
import com.chixing.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chixing.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.UUID;

/**
 * <p>
 * 流水表 服务实现类
 * </p>
 *
 * @author smith
 * @since 2025-09-22
 */
@Service
public class FlowServiceImpl implements IFlowService {
    @Autowired
    private FlowMapper flowMapper;
    @Autowired
    private IMyorderService myorderService;
    @Autowired
    private IMycouponService mycouponService;
    @Autowired
    private IReceiveAddressService receiveAddressService;
    @Autowired
    private ICouponService couponService;

    @Override
    public Result getById(Integer flowId) {
        Flow flow = flowMapper.selectById(flowId);
        if (flow!=null)
            return Result.getSuccess(flow);
        return Result.getFail(null);
    }

    // 支付： （1） 添加flow 记录  （2）修改订单状态成功  （3）修改使用的优惠券的状态
    @Override
    public Result save(Flow flow) {
        flow.setFlowNum(UUID.randomUUID().toString().replace("-",""));
        flow.setBuyCounts(1);//默认购买数量是1，根据业务，若有数量则使用具体数量
        flow.setCreateTime(LocalDateTime.now());
        flow.setStatus(0);

        //1.添加flow记录
        int rows = flowMapper.insert(flow);
        if (rows>0){
            System.out.println("支付成功，支付记录是：" + flow);
            //2.修改订单状态
            Result updateOrderResult = myorderService.updateStatus(flow.getOrderId(), 1);
            if (updateOrderResult.getCode() == 200){
                System.out.println("修改订单状态成功---------order id = " + flow.getOrderId());
                Result myOrderResult = myorderService.getById(flow.getOrderId());
                Myorder myorder = (Myorder) myOrderResult.getData();
                //3.修改使用的优惠券状态
                Result mycouponResult = mycouponService.updateStatus(myorder.getMycouponId(), 1);
                if (mycouponResult.getCode() == 200)
                    return Result.saveSuccess(flow);
            }
        }
        return Result.saveFail(flow);
    }


    //查看流水详情，流水flow + 订单myorder + 收件地址 address +优惠券mycoupon+coupon
    @Override
    public Result getDetailById(Integer flowId) {
        //1.流水flow信息
        Flow flow = flowMapper.selectById(flowId);
        if (flow!=null){
            //2.查订单信息
            Myorder myorder = (Myorder) myorderService.getById(flow.getOrderId()).getData();
            //3.查收获地址信息
            ReceiveAddress address = (ReceiveAddress) receiveAddressService.getById(myorder.getAddressId()).getData();
            //4.查优惠券信息
            Mycoupon mycoupon = (Mycoupon) mycouponService.getById(myorder.getMycouponId()).getData();
            Coupon coupon = (Coupon) couponService.getById(mycoupon.getCouId()).getData();


            HashMap<String, Object> map = new HashMap<>();
            map.put("flowNumber" , flow.getFlowNum());
            map.put("paidPrice" , flow.getPaidPrice());
            map.put("flowCreateTime" , flow.getCreateTime());
            map.put("flowStatus" , flow.getStatus());

            map.put("orderNumber" , myorder.getOrderNum());
            map.put("orderPrice" , myorder.getOrderPrice());
            map.put("orderCreateTime" , myorder.getOrderCreateTime());
            map.put("orderStatus" , myorder.getOrderStatus());
            map.put("productName" , myorder.getProductName());
            map.put("productPrice" , myorder.getProductPrice());
            map.put("productImg" , myorder.getProductImg());


            String addressInfo = address.getAddrProvince()+address.getAddrCity()+address.getAddrArea()+address.getAddrDetail();
            map.put("addressInfo" , addressInfo);
            map.put("receiveUsername" , address.getReceiveUsername());
            map.put("receiveUserTelon" , address.getReceiveUserTelno());

            map.put("couponName",coupon.getCouName());
            map.put("couponPrice",coupon.getCouMoney());

            return  Result.getSuccess(map);
        }
        return Result.getFail(flowId);
    }
}
